package com.gexin.weixin.interceptor;
import com.gexin.common.AppGlobal;
import com.gexin.common.utils.WebUtils;
import com.gexin.model.weixin.WeixinAppConfig;
import com.gexin.weixin.kit.WxAppConst;
import com.gexin.weixin.kit.WxConfigKit;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
/**
 * 微信授权的拦截器
 * @author 肖得明
 *2015-11-28
 *
 */
public class WexinAuthInterceptor implements Interceptor {

	@Override
	public void intercept(Invocation paramInvocation) {
		Controller controller = paramInvocation.getController();
		Integer   this_appid = 1; //当前授权的
    	Integer   current_id = controller.getCookieToInt(WxAppConst.Current_Weixin_Cookie_AppId);
		if((current_id ==null || !current_id.equals(this_appid)) || controller.getCookie(WxAppConst.Weixin_UserCookie_Id)==null){
			String url = WebUtils.getRealUrl(AppGlobal.Host_Domain_Name,paramInvocation);
			controller.setSessionAttr(WxAppConst.Wexin_Auth_redirect,url);
			WeixinAppConfig  config =  WxConfigKit.cache_getConfigById(this_appid);
			controller.redirect(config.getRegAuthUrl());
		}else{
			paramInvocation.invoke(); 
		}
	}
	
	

}
